<template>
 <div class="sidebar-container" @mouseenter="showSidebar" @mouseleave="hideSidebar">
<!-- 触发区域 -->
<div class="sidebar-trigger">
    <div class="trigger-icon">
        <span></span>
        <span></span>
        <span></span>
      </div>
</div>
<!-- 侧边栏内容区域 -->
    <div class="sidebar" :class="{ 'sidebar-open': isOpen }">
        <div class="sidebar-header">
            <h3>导航菜单</h3>
        </div>

        <nav class="sidebar-nav">
            <router-link to="/karry"
            class="nav-item"
            @click="closeSidebar"
            >
            <div class="nav-icon">👤</div>
            <span class="nav-text">名人动态</span>
            </router-link>

            <router-link to="/chart"
            class="nav-item"
            @click="closeSidebar">
            <div class="nav-icon">💬</div>
            <span class="nav-text">社交大厅</span>
            </router-link>

            <router-link to="/yourslife" 
            class="nav-item"
            @click="closeSidebar"
            >
            <div class="nav-icon">📸</div>
            <span class="nav-text">晒晒生活</span>
            </router-link>

            <router-link to="/mycontents" 
            class="nav-item"
            @click="closeSidebar"
            >
            <div class="nav-icon">🛒</div>
            <span class="nav-text">商城</span>
            </router-link>

            <router-link to="/login" 
            class="nav-item"
            @click="closeSidebar"
            >
            <div class="nav-icon">🔐</div>
            <span class="nav-text">登录页面</span>
            </router-link>
        </nav>

        <div>
          <!-- localStorage方式 -->
          <h3>用户名1:{{ username }}</h3>
          <!--  Vuex状态管理方式 -->
          <h3>用户名2:{{ this.$store.state.loginName }}</h3>
          <!-- sessionStorage方式 -->
          <h3>用户名3:{{ username3}}</h3>
        </div>
    </div>
</div>

</template>

<script>
import { ref } from 'vue';
export default {
  data(){
    return {
      // username用与显示
      username:'无',
      username3:'zyy3',
      isOpen: false
    }
  },
  mounted(){
    this.username=localStorage.getItem('username');
    this.username3=sessionStorage.getItem('username');
    // 移除不存在的mutation调用
    // this.$store.commit('moddate');
  },
  methods: {
    // 侧边栏开关触发
    showSidebar() {
      this.isOpen = true;
    },
    hideSidebar() {
      this.isOpen = false;
    },
    // 点击导航链接时关闭侧边栏
    closeSidebar() {
      this.isOpen = false;
    }
  }
}
</script>
<style scoped>
.sidebar-container {
  position: fixed;
  left: 0;
  top: 0;
  height: 100vh;
  z-index: 1000;
  display: flex;
}

.sidebar-trigger {
  width: 20px;
  height: 100vh;
  background: #e3f2fd;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  border-right: 1px solid #bbdefb;
}

.sidebar-trigger:hover {
  width: 25px;
  background: #bbdefb;
}

.trigger-icon {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.trigger-icon span {
  width: 12px;
  height: 2px;
  background: #1976d2;
  border-radius: 1px;
  transition: all 0.3s ease;
}

.sidebar {
  width: 0;
  height: 100vh;
  background: white;
  box-shadow: 2px 0 15px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: all 0.3s ease;
  border-right: 1px solid #e0e0e0;
}

.sidebar-open {
  width: 250px;
}

.sidebar-header {
  padding: 20px;
  background: #e3f2fd;
  color: #1976d2;
  text-align: center;
  border-bottom: 1px solid #bbdefb;
}

.sidebar-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.sidebar-nav {
  padding: 20px 0;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  text-decoration: none;
  color: #333;
  transition: all 0.3s ease;
  border-left: 3px solid transparent;
}

.nav-item:hover {
  background: #e3f2fd;
  border-left-color: #1976d2;
  color: #1976d2;
  transform: translateX(5px);
}

.nav-item.router-link-active {
  background: #e3f2fd;
  border-left-color: #1976d2;
  color: #1976d2;
  font-weight: 600;
}

.nav-icon {
  font-size: 20px;
  margin-right: 15px;
  width: 24px;
  text-align: center;
}

.nav-text {
  font-size: 16px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar-open {
    width: 200px;
  }
  
  .nav-item {
    padding: 12px 15px;
  }
  
  .nav-text {
    font-size: 14px;
  }
}

/* 动画效果 */
.sidebar-container:hover .sidebar {
  width: 250px;
}

.sidebar-container:hover .sidebar-trigger {
  width: 25px;
}
</style>
